<template>
  <div class="app-container  dialog-w80">
    <div class="customContainer">
      <el-form :inline="true" :model="formInline" class="demo-form-inline" size="small">
        <el-form-item>
          <el-select filterable placeholder="选择门店" v-model="formInline.store_id">
            <el-option v-for="item in store_list" :key="item.id" :label="item.name" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-input v-model="formInline.gift_id" placeholder="请输入礼品编号" @keyup.enter.native="searchClick"></el-input>
        </el-form-item>
        <el-form-item>
          <dj-button size="small" @search="searchClick">
            <template #default>搜索</template>
          </dj-button>
        </el-form-item>
      </el-form>

      <!-- 列表 -->
      <el-table highlight-current-row :data="list" border @sort-change='sortChange' size="small">
        <el-table-column align="center" label="礼品图片" width="80">
          <template slot-scope="scope">
            <img :src="scope.row.thumb" width="60" height="60" alt="">
          </template>
        </el-table-column>
        <el-table-column align="center" label="礼品名称" prop="name"></el-table-column>
        <el-table-column align="center" label="新礼品编号" prop="gift_id"></el-table-column>
        <el-table-column align="center" label="神采礼品编号" sortable>
          <template slot-scope="scope">
            {{scope.row.no_obsolete}}
          </template>
        </el-table-column>
        <el-table-column align="center" label="箱规" prop="box_num"></el-table-column>
        <el-table-column align="center" label="尺寸" prop="size"></el-table-column>
        <el-table-column align="center" label="类别" prop="type_name"></el-table-column>
        <el-table-column align="center" label="成本价" prop="money_cost"></el-table-column>
        <el-table-column align="center" label="库存量" sortable>
          <template slot-scope="scope">
            {{scope.row.num}}
          </template>
        </el-table-column>
        <el-table-column align="center" label="前台销售">
          <template slot-scope="scope">
            {{scope.row.is_store_on_sale==1?'支持':'-'}}
          </template>
        </el-table-column>
        <el-table-column align="center" label="彩票兑换">
          <template slot-scope="scope">
            {{scope.row.is_ticket_convertible==1?'支持':'-'}}
          </template>
        </el-table-column>
        <el-table-column align="center" label="积分兑换">
          <template slot-scope="scope">
            {{scope.row.is_integral_convertible==1?'支持':'-'}}
          </template>
        </el-table-column>
        <!-- 列表的操作按钮 -->
        <!-- 大佬，，，弹框输入数量 -->
        <el-table-column align="center" label="加入调拨">
          <template slot-scope="scope">
            <el-button style="font-size:28px" :disabled="scope.row.in_car==1" type="text" @click="addcar(scope.row.id,1,formInline.store_id,scope.$index)">
              <i class="el-icon-circle-plus"></i>
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 礼品调拨审单弹框 -->
    <el-dialog :close-on-click-modal="false" class="dialog_table" title="礼品调拨审单" :visible.sync="show_carList" @close="car_close">
      <div class="customHeader">
        <el-select placeholder="请选择收货地址" filterable v-model="carStoreVal">
          <el-option v-for="item in carStoreList" :key="item.house_id" :label="item.store_name" :value="item.store_id">
          </el-option>
        </el-select>
      </div>
      <el-table highlight-current-row :data="carList" v-loading.body="listLoading" border size="small">
        <el-table-column align="center" label="图片" width="70">
          <template slot-scope="scope">
            <img :src="scope.row.thumb" width="50" height="50" alt="">
          </template>
        </el-table-column>
        <el-table-column align="center" label="名称" prop="name"></el-table-column>
        <el-table-column align="center" label="礼品编号" prop="gift_id"> </el-table-column>
        <el-table-column align="center" label="箱规" prop="box_num"></el-table-column>
        <el-table-column align="center" label="库存量" prop="num"></el-table-column>
        <el-table-column align="center" label="调拨数量(个)">
          <template slot-scope="scope">
            <el-input onkeypress="return event.which>=48&&event.which<=57" type="number" onmousewheel="return false;" v-model="scope.row.gift_num"
              placeholder="数量" @input="count"></el-input>
          </template>
        </el-table-column>
        <el-table-column align="center" label="成本价">
          <template slot-scope="scope">
            <!-- 保留小数 -->
            {{scope.row.money_cost|keepTwoNum}}
          </template>
        </el-table-column>
        <el-table-column align="center" label="金额总计">
          <template slot-scope="scope">
            <!-- 保留小数 -->
            {{scope.row.money_cost*scope.row.gift_num|keepTwoNum}}
          </template>
        </el-table-column>
        <el-table-column align="center" label="操作">
          <template slot-scope="scope">
            <el-button @click="delClick(scope.row.gift_id,formInline.store_id,formInline.house_id,scope.$index)" type="danger" icon="el-icon-delete"
              size="mini"></el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="bottom_box">
        <el-button type="text" size="small" @click="delCarListClick(formInline.store_id,formInline.house_id)">清空调拨单
        </el-button>
        <el-button class="pull-right" size="small" type="primary" @click="saveClick()">确认调拨</el-button>
        <p class="pull-right" style="line-height:0">礼品种类:{{car_gift_num}} 　调拨单礼品数:{{totalOrder}}
          　合计:{{totalPrice|keepTwoNum}}</p>
      </div>
    </el-dialog>
    <!-- 自定义底部 -->
    <div class="bottom_box">
      <el-pagination class="pull-left" background @current-change="handleCurrentChange" :current-page.sync="currentPage" :page-size="20"
        layout="prev, pager, next, jumper" :total="totalDataNumber">
      </el-pagination>
      <el-badge :value="car_gift_num" class="item pull-right">
        <el-button type="text" size="small" @click="goodscar(formInline.store_id,1)">
          <svg-icon style="font-size:18px" icon-class="car" />
        </el-button>
      </el-badge>
    </div>
  </div>
</template>

<script>
import {
  getBrandList,
  store,
  getList,
  getPage,
  addcar_submit,
  get_type,
  get_size,
  car,
  delGift,
  delCarList,
  save,
  carStore
} from '@/api/storage_control/add_out_allocating.js';
export default {
  data() {
    return {
      currentPage: 1,
      totalDataNumber: 0,
      first_type_list: null,
      second_type_list: null,
      listLoading: false,
      type_list: null,
      size_list: null,
      formInline: {
        store_id: '',
        gift_id: '',
        order_by: ''
      },
      store_list: null,
      house_list: [],
      list: null,
      car_gift_num: 0,
      carList: [],
      show_carList: false,
      carStoreList: [],
      carStoreVal: '',
      saveForm: {},
      totalOrder: '',
      totalPrice: ''
    };
  },
  beforeRouteEnter(to, from, next) {
    next(vm => {
      //当进入组件之前，执行 beforRouteEnter 路由钩子函数 ,//参数vm就是当前组件的实例。
      if (!vm.store_list) {
        vm.getstore();
      }
    });
  },
  methods: {
    sortChange(column, prop, order) {
      this.listLoading = true;
      if (column.order == 'ascending') {
        if (column.column.label == '神采礼品编号') {
          this.formInline.order_by = 1;
        } else {
          this.formInline.order_by = 3;
        }
      } else if (column.order == 'descending') {
        if (column.column.label == '神采礼品编号') {
          this.formInline.order_by = 2;
        } else {
          this.formInline.order_by = 4;
        }
      } else {
        this.formInline.order_by = '';
      }
      this.fetchData();
    },
    getstore() {
      store({ category_id: 170 }).then(response => {
        this.store_list = response;
        let data = response.filter(res => res.id == this.$store.getters.optionId);
        if (data.length > 0) {
          this.formInline.store_id = data[0].id;
        }
        this.fetchData();
      });
    },
    searchClick() {
      if (this.formInline.store_id) {
        this.fetchData();
      } else {
        this.$message({ type: 'error', message: '请选择门店' });
      }
    },
    goodscar(store_id, type) {
      this.listLoading = true;
      car({ store_id: store_id, type: type })
        .then(response => {
          response.forEach(function(val, i) {
            val.money_cost = ((val.money_cost * 1) / 1000000).toFixed(2);
          });
          this.carList = response;
          this.listLoading = false;
          this.show_carList = true;
          this.count();
        })
        .catch(res => {
          this.listLoading = false;
        });
      carStore({}).then(response => {
        this.carStoreList = response;
      });
    },
    //确认调拨
    saveClick() {
      if (this.carList.length == 0) {
        this.$message({ type: 'error', message: '大佬，你购物车空空如也' });
        return;
      }
      this.saveForm = {
        store_id_in: this.carStoreVal,
        store_id_out: this.formInline.store_id,
        order: [],
        type: 2
      };
      let err = false;
      let errNum = false;
      if (!this.saveForm.store_id_in) {
        this.$message({ type: 'error', message: '选择收货地址' });
        return;
      }
      if (this.saveForm.store_id_in == this.saveForm.store_id_out) {
        this.$message({ type: 'error', message: '选择正确的收货地址' });
        return;
      }
      for (var k = 0; k < this.carList.length; k++) {
        if (this.carList[k].gift_num * 1 > this.carList[k].num * 1) {
          err = true;
          this.$message({ type: 'error', message: '调拨数不可大于当前库存数' });
          break;
        }
      }
      if (err) {
        return;
      }
      for (var i = 0; i < this.carList.length; i++) {
        if (this.carList[i].gift_num == '' || this.carList[i].gift_num * 1 < 0 || !/^\d+$/.test(this.carList[i].gift_num)) {
          errNum = true;
          this.$message({
            type: 'error',
            message: '请将调拨数填写完整,并且调拨数只能为整数!'
          });
          break;
        }
      }
      if (errNum) {
        return;
      }
      for (var i = 0; i < this.carList.length; i++) {
        this.saveForm.order.push({
          gift_id: this.carList[i].gift_id,
          order_num: this.carList[i].gift_num,
          look_money: this.carList[i].money_cost * 1000000,
          money_cost: this.carList[i].money_cost * 1000000
        });
      }
      save(this.saveForm).then(response => {
        this.$message({
          type: response.code == 1 ? 'success' : 'error',
          message: response.message
        });
        if (response.code == 1) {
          this.show_carList = false;
        }
      });
    },
    // 计算购物车
    count() {
      var totalPrice = 0; //临时总价
      var totalOrder = 0;
      this.carList.forEach(function(val, index) {
        totalPrice += val.gift_num * 1 * val.money_cost * 1; //累计总价
        totalOrder += val.gift_num * 1;
      });

      this.totalPrice = parseFloat(totalPrice);
      this.totalOrder = parseFloat(totalOrder);
    },
    addcar(gift_id, num, store_id, index) {
      var data = {
        gift_id: gift_id,
        type: 1,
        num: num,
        store_id: store_id
      };
      this.listLoading = true;
      addcar_submit(data).then(response => {
        this.listLoading = false;
        if (response.code == 1) {
          this.list[index].in_car = 1;
          this.car_gift_num += 1;
          this.$message({ type: 'success', message: '添加成功' });
        }
      });
    },
    // 清空购物车
    delCarListClick(store_id, house_id) {
      delCarList({ store_id: store_id, type: 1 }).then(response => {
        this.carList = [];
      });
    },
    delClick(gift_id, store_id, house_id, index) {
      delGift({
        gift_id: gift_id,
        house_id: house_id,
        store_id: store_id,
        type: 1
      }).then(response => {
        this.$message({
          type: response.code == 1 ? 'success' : 'error',
          message: response.message
        });
        if (response.code == 1) {
          this.car_gift_num--;
          this.carList.splice(index, 1);
          this.count();
        }
      });
    },
    car_close() {
      this.handleCurrentChange(this.currentPage);
    },
    first_type() {
      get_type({ id: 0 }).then(response => {
        this.first_type_list = response;
      });
    },
    second_type(id) {
      this.listLoading = true;
      this.formInline.second_type = '';
      this.formInline.type = '';
      get_type({ id: id }).then(response => {
        this.second_type_list = response;
        this.listLoading = false;
      });
    },
    type(id) {
      this.formInline.type = '';
      this.listLoading = true;
      get_type({ id: id }).then(response => {
        this.type_list = response;
        this.listLoading = false;
      });
    },

    // 列表获取
    fetchData() {
      this.listLoading = true;
      this.currentPage = 1;
      getPage(this.formInline).then(response => {
        this.totalDataNumber = response * 20;
        getList(this.formInline).then(response => {
          this.car_gift_num = 0;
          this.car_gift_num = response.length > 0 ? response[0].car_gift_num : 0;
          this.list = response;
          this.listLoading = false;
        });
      });
    },
    // 分页
    handleCurrentChange(val) {
      this.car_gift_num = 0;
      this.listLoading = true;
      getList(this.formInline, val).then(response => {
        if (response.length > 0) {
          this.car_gift_num = response[0].car_gift_num;
        }
        this.list = response;
        this.listLoading = false;
      });
    }
  }
};
</script>
<style>
.dialog-w80 .el-dialog {
  width: 80%;
}
</style>